package org.springframework.zp.lifeCycle.bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.PostConstruct;

//@Component
@Slf4j(topic = "e")
public class A implements BeanNameAware, ApplicationContextAware, InitializingBean {

	/**
	 * 1.推断构造方法
	 */
	public A() {
		log.debug("1-A create constructor");
	}

	/**
	 * 2.依赖注入
	 * 	 @Autowired 注解方式依赖注入
	 */
	@Autowired
	B field;

	C c;

	public void m0() {
		log.debug("am0");
	}

	/**
	 * 3.依赖注入
	 * 	属性自动填充，使用 java 内省机制
	 *
	 * @param c
	 */
	public void setC(C c) {
		log.debug("3-bytype wired:[{}]", c);
		this.c = c;
	}

	/**
	 * 4.执行部分 aware 的回调
	 *
	 * @param name the name of the bean in the factory.
	 * Note that this name is the actual bean name used in the factory, which may
	 * differ from the originally specified name: in particular for inner bean
	 * names, the actual bean name might have been made unique through appending
	 * "#..." suffixes. Use the {@link BeanFactoryUtils#originalBeanName(String)}
	 * method to extract the original bean name (without suffix), if desired.
	 */
	@Override
	public void setBeanName(String name) {
		log.debug("4-setBeanName:[{}]", name);
	}


	/**
	 * 5.执行部分 aware 的回调 执行的生命周期回调
	 *
	 * @param applicationContext the ApplicationContext object to be used by this object
	 * @throws BeansException
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		log.debug("5-setApplicationContext:[{}]", applicationContext);
	}


	/**
	 * 6.执行的生命周期的回调
	 */
	@PostConstruct
	public void postInit() {
		log.debug("6-PostConstruct init lifecycle");
	}


	/**
	 *7.执行接口版的生命周期回调,即 BeanPostProcessor 对象的 postProcessAfterInitialization() 方法
	 *
	 * @throws Exception
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		log.debug("7-afterPropertiesSet init lifecycle");
	}
}
